/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.filter;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 *
 * @author joao
 */
//@WebListener
public class UsuarioDuplicadoListener implements HttpSessionAttributeListener {
    private static final String ATTRIBUTE_NAME = "usuarioAutenticado";
    private static final Set<Object> USUARIOS_LOGADOS = Collections.synchronizedSet(new HashSet<>());
 
    @Override
    public void attributeAdded(HttpSessionBindingEvent bind) {
        String attributeName = bind.getName();
        Object attributeValue = bind.getValue();
        if(ATTRIBUTE_NAME.equals(attributeName) ) {
            if(!USUARIOS_LOGADOS.add(attributeValue)){
                bind.getSession().removeAttribute(ATTRIBUTE_NAME);
            }
        }
    }
 
    @Override
    public void attributeRemoved(HttpSessionBindingEvent bind) {
        String attributeName = bind.getName();
        Object attributeValue = bind.getValue();
        if(ATTRIBUTE_NAME.equals(attributeName)) {
            USUARIOS_LOGADOS.remove(attributeValue);
        }
    }
 
    @Override
    public void attributeReplaced(HttpSessionBindingEvent bind) {
    }
}
